//继承模板
{extend name="public/base"}


//CSS
{block name="style"}
<link rel="stylesheet" href="__ADMIN_STATIC__/lib/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
{/block}



//正文区域
{block name="body"}{//---------------------------------------------------------------------------}
<div class="panel-wrap page-zzjg">
	<div class="panel-left">
    	<div class="panel-title cl">
        	<div class="l">
        	<button class="btn radius Hui-iconfont Hui-iconfont-fenlei" type="button" onclick="expandAll(this)"> 展开</button> 
        	</div>
            <div class="r">
                <button class="btn btn-success  radius" type="button" title="刷新" onclick="btn_zTree_reload();"><i class="Hui-iconfont">&#xe68f; </i>刷新</button>
            </div>	
        </div>
        <div class="tree">
            	<ul id="treeDemo" class="ztree"></ul>
        </div>
    	
    </div>
    <div class="panel-right">
        <iframe id="iframe1" frameborder="0" src="" style="position: absolute;bottom: 0;height: 100%;width: 100%; "></iframe>
    </div>
</div>
{/block}
//-----------------------------------------------------------------------------------------------


<!--请在下方写此页面业务相关的脚本-->
//-----------------------------------------------------------------------------------------------
{block name="script"}
<script type="text/javascript">

;(function($){
	//alert('666');
})(jQuery);

var iframe1 = $('#iframe1');
function openiframe( id ){
    iframe1.attr('src' , '{:url('lists')}?dept_id='+id  ) ;
}

var zTree , zNodes = [];
var t = $("#treeDemo");
var setting = {
	view: {
		dblClickExpand: false,  //双击展开
		showLine: true,        //是否显示 连接线
		selectedMulti: false,   //设置是否允许同时选中多个节点。默认值: true
		//showIcon:true , //是否显示图标
	},
	data: {
		key:{
			name: "dept_name",
		},
		simpleData: {
			enable:true, 
			idKey: "dept_id",
			pIdKey: "fid",
			rootPId: ""
		},
		

	},
	callback: {
		beforeClick:beforeClick, 
        onExpand: onExpand,  // 展开
        onCollapse: onCollapse,  // 折叠  


	}
};

// 展开
function onExpand(event, treeId, treeNode){  
    var cookie = $.cookie("z_tree");  
      
    var z_tree = null;  
    if(cookie){  
        z_tree = JSON.parse(cookie);  
    }  
      
    if(!z_tree){  
        z_tree = new Array();  
    }  
    if(jQuery.inArray(treeNode.dept_id, z_tree)<0){  
        z_tree.push(treeNode.dept_id); //追加 
    }  
      
      
    $.cookie("z_tree", JSON.stringify(z_tree))  
}  
// 折叠  
function onCollapse(event, treeId, treeNode){  
    var cookie = $.cookie("z_tree");  
      
    var z_tree = null;  
    if(cookie){  
        z_tree = JSON.parse(cookie);  
    }  
      
    if(!z_tree){  
        z_tree = new Array();  
    }  
    var index = jQuery.inArray(treeNode.dept_id, z_tree);  
      
    z_tree.splice(index, 1); //删除  
      
    $.cookie("z_tree", JSON.stringify(z_tree))  
}


function beforeClick(treeId, treeNode, clickFlag) {
	console.log('beforeClick:' ,treeId, treeNode , clickFlag);
    openiframe( treeNode.dept_id );
}

function zTree_reload(ck=null){
	var url_notes = "{:url('dept/zNodes')}";//获取url 


	jBox.tip('loading...' , 'loading');
    $.ajax({
        type:'post',
        url:url_notes,
        data: {},
        dataType:'json',
        success:function(d){

        		jBox.closeTip();
        		zNodes = d;//
        		zTree_init();

               if( typeof(ck) == 'function' ){ ck(); };

        },
        error:function(a,b,c){
            jBox.tip('ajax error：' + b );
        }
    });






}

function zTree_init(){
		


	zTree = $.fn.zTree.init(t, setting, zNodes);
	var cookie = $.cookie("z_tree");  
    if(cookie){  
        z_tree = JSON.parse(cookie);  
        for(var i=0; i< z_tree.length; i++){  
	        var node = zTree.getNodeByParam('dept_id', z_tree[i])  
	        zTree.expandNode(node, true)  
        }  
    }

    return zTree;
}


//展开折叠切换
var flag_expand = false;
function expandAll(obj){
	if(! $(obj).length ) return false ;
	var z_tree = [];

	if( flag_expand ){
		$(obj).text(' 展开');
		//z_tree = [];

	}else{
		$(obj).text(' 折叠');
		for(var i = 0 , len = zNodes.length ; i < len  ;  i++  ){
			z_tree.push( zNodes[i].dept_id );
		}
	}

	$.cookie("z_tree", JSON.stringify(z_tree));
	zTree.expandAll(flag_expand = !flag_expand);
	// var node = zTree.getNodeByParam("dept_id", 5);
	// zTree.selectNode(node, true, true);
	// zTree.checkNode(node, true, true);

}
function btn_zTree_reload(){
	zTree_reload();
}

//删除按钮
function btn_delNode(){

    var url_del = '{:url('delete')}';

    var node = zTree.getSelectedNodes();
        if(!node || !node.length ){
            jBox.tip('请选择要删除的部门~~~');return;
        }
        node = node[0] ;

    var doDel = function(){
        jBox.tip('处理中...' , 'loading');  

        $.ajax({
            type:'post',
            url:url_del,
            data: {dept_id : node.dept_id},
            dataType:'json',
            success:function(d){
            	if(d.ret == 0 ){
            		top.jBox.tip( '删除成功！' + d.msg + '!!', 'success' );
            		setTimeout(function(){
						// window.location.replace(location.href);
						// window.location.href=location.href
						//location.reload();
						zTree_reload();
            		},3000);
            	}else{
            		top.jBox.tip( d.msg , 'error' );
            	}
            },
            error:function(a,b,c){
                jBox.tip('ajax error：' + b );
            }
        });
    }
    

    var submit = function (v, h, f) {
        if (v == 'ok'){
            doDel();
        }else if (v == 'cancel'){
            jBox.tip('取消...', 'info');
        }
        return true; //close
    };

    $.jBox.confirm("确定 删除吗？", "提示", submit);


}



$(document).ready(function(){

	zTree_reload(function(){
        //zTree加载完成 设置第一个被选中
        var node = zTree.getNodes()[0];
        zTree.selectNode(node);
        setting.callback.beforeClick('treeDemo ', node, 1);
    });




                
	$('#form1').validate({
		onkeyup:false, //在 keyup 时验证。
        onclick:true, // 在点击复选框和单选按钮时验证。
        onfocusout:false, //失去焦点时验证（不包括复选框/单选按钮）。
        onsubmit:true ,   // 提交时验证。设置为 false 就用其他方法去验证。
		focusCleanup:true,focusInvalid:false,
		//success:"valid", //验证成功 添加样式
		//
		rules: {
	      	fname:{
	        	required:function(a,b,c){
	        		return !$('[name=fid]').val();
	        	},
		    },
      	},
		submitHandler:function(form){
			
            jBox.tip('loading...' , 'loading') ; 
			
			//http://www.h-ui.net/lib/jQuery.form.js.shtml
			var a = $(form).ajaxSubmit({
				success:function(d){
                    console.log(d);
                    console.log( d.ret != 0  );
					if( d.ret == 0  ){
                        
                        top.jBox.tip( '保存成功' + d.msg + '!!', 'success' );
	            		setTimeout(function(){
							// window.location.replace(location.href);
							// window.location.href=location.href
							// location.reload();
							zTree_reload();
							setFormReset()
							
	            		},2000);
                    }else{
                        jBox.tip( d.msg ,'error');
                    }

				},
                error:function(){
                    alert('ajax error:');
                    jBox.closeTip();
                }

			});

			
			//location.replace(location.href);
			
			//parent.$('.btn-refresh').click();
			//var index = parent.layer.getFrameIndex(window.name);
			//parent.layer.close(index);
		},
		showErrors:function(errorMap,errorList) {
			this.defaultShowErrors(); //默认验证 
            // 判断 错误数量  
			if( this.numberOfInvalids() ){
				h.tip( '有' + this.numberOfInvalids() + '个错误，请检查！' ,'error');
			}
    	}
	});



	
	
	// $( '[deptselect]' ).deptselect({id:100});


});






</script>

<style>

#think_page_trace{ display:none}

	
</style>



{/block}{//---------------------------------------------------------------------------}